package com.thwj.project.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.thwj.project.app.service.AppActivityService;
import com.thwj.project.biz.domain.Activity;
import com.thwj.project.biz.domain.ActivitySignup;
import com.thwj.project.biz.domain.User;
import com.thwj.project.biz.enums.ActivitySignUpStatusEnum;
import com.thwj.project.biz.enums.ActivityStatusEnum;
import com.thwj.project.biz.mapper.ActivityMapper;
import com.thwj.project.biz.service.IActivityService;
import com.thwj.project.biz.service.IActivitySignupService;
import com.thwj.project.biz.service.IUserService;
import com.thwj.project.push.enums.PushTemplateEnumNew;
import com.thwj.project.push.event.WxMpMessageEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 活动Service业务层处理
 *
 * @author generate
 * @date 2023-06-05
 */
@Service
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity> implements IActivityService {

    @Autowired
    IActivitySignupService activitySignupService;
    @Autowired
    AppActivityService appActivityService;
    @Autowired
    IUserService userService;
    @Autowired
    ApplicationEventPublisher applicationEventPublisher;

    @Override
    public int cancel(Activity activity) {

        // 查询已报名的数据
        List<ActivitySignup> activitySignupList = activitySignupService.list(
                new QueryWrapper<ActivitySignup>()
                        .eq("activity_id", activity.getId())
                        .eq("status", ActivitySignUpStatusEnum.sign_up_ed.getValue())
        );
        // 发起退款
        activitySignupList.forEach(activitySignup->{
            try {
                // 当前如果已经不是报名成功的状态 不考虑
                if(ActivitySignUpStatusEnum.sign_up_ed.getValue() != activitySignup.getStatus()) {
                    return;
                }
                appActivityService.singleActivityRefund(activitySignup, activity);
                // 通知 报名的用户
                User user = userService.getById(activitySignup.getUserId());
//                applicationEventPublisher.publishEvent(WxMpMessageEvent.builder()
//                        .miniAppPath("pages/detail/detail?activityId=" + activitySignup.getActivityId())
//                        .userId(user.getId())
//                        .pushTemplateEnum(PushTemplateEnum.ACTIVITY_SIGN_UP_CANCEL)
//                        .templateDataList(PushTemplateEnum.getACTIVITY_SIGN_UP_CANCEL_Data("你的预约已被取消", activity.getTitle(), activitySignup.getCreateDate()))
//                        .build()
//                );

                applicationEventPublisher.publishEvent(WxMpMessageEvent.builder()
                        .miniAppPath("pages/detail/detail?activityId=" + activitySignup.getActivityId())
                        .userId(user.getId())
                        .pushTemplateEnum(PushTemplateEnumNew.ACTIVITY_CANCEL_SUCCESS)
                        .templateDataList(
                                PushTemplateEnumNew.ACTIVITY_CANCEL_SUCCESS(
                                        activity.getTitle(),
                                        "组局人" + activity.getContactName(),
                                        activity.getContactPhone(),
                                        activity.getContactPhone()))

                        .build()
                );
            }catch (Exception e){
                log.error("单笔退款发生异常 ：{}",e);
            }
        });
        this.update(new UpdateWrapper<Activity>()
                .set("status", ActivityStatusEnum.cancel.getValue())
                .set("cancel_date",new Date())
                .eq("id",activity.getId()));

        return 1;
    }
}
